<?php

namespace app\api\controller\v27;

use app\api\controller\v2\Auth;
use app\common\cachemodel\live\LiveGoodsActivity;
use app\common\cachemodel\live\LiveRecordingInfo;
use app\common\cachemodel\ZhunleActivity;
use app\common\model\Recode;
use think\response\Json;

/**
 * @version 2.7.0 卡券控制器
 * Class Coupon
 * @package app\api\controller\v27
 */
class Coupon extends Auth
{
    protected $gidDefault = [
        1 => 1,
        2 => 2,
        3 => 20,
        4 => 24,
        5 => 19,
    ];

    /**
     * 下单获取可用优惠券数量
     * @return Json
     * @throws \Exception
     */
    public function getUsableNum()
    {
        $gids = input('gids/s', '');
        $gidsArr = explode(',', $gids);
        $gid = (int)current($gidsArr);
//        $goods_type = input('goods_type/d', 0);
        $start_time = input('start_time/s', '');
        $rid = input('rid/d', 0);
        $rid2 = input('rid2/d', 0);
        $liveId = input('live_id/d', 0);
        $liveRecordId = input('live_record_id/d', 0);
        (new LiveRecordingInfo())->checkRecordId($liveId, $liveRecordId);
        if ($rid)
        {
            $recode = (new Recode())->getRecode(['id' => $rid]);
            if (!$recode)
            {
                return show(config('code.error'), '档案不存在');
            }
        }
        $GoodsModel = new \app\common\cachemodel\Goods();

        //数据初始化
        $arr = [
            'num' => 0
        ];
        if (!current($gidsArr))
        {
            return show(config('code.success'), '请求成功', $arr);
        }
        $goods = $GoodsModel->batchGetInfoByIds($gidsArr);
        $goodsTypes = array_unique(array_column($goods, 'type'));
        //只能验证一种商品类型
        if (count($goodsTypes) !== 1)
        {
            return show(config('code.error'), '请求异常');
        }
        $goods_type = (int)current($goodsTypes);
        if ($goods_type === 17)
        {
            $goods = $GoodsModel->getSynastryReportGoods($gid);
        }
        (new ZhunleActivity())->goodsActiveInfo($goods_type, $goods, $start_time);
        if ($liveId)
        {
            (new LiveGoodsActivity())->getLiveOrderTypeGoods($liveId, $goods_type, $goods, ['isCloseActive' => true]);
        }
        $goods_info = (new \app\common\cachemodel\Goods())->resetGoodsInfo2($this, $goods_type, $goods, $rid, $rid2, $start_time, $gidsArr);
        $arr['num'] = $goods_info['coupon_data']['usable_num'];
        if ($goods_type === 17 && $start_time)
        {
            $synastryReportGoods = (new \app\common\cachemodel\Goods())->GoodsTypeInfo(17);
            foreach ($synastryReportGoods as $key => $item)
            {
                if (!isset($item['good_list'][$gid]))
                {
                    continue;
                }
                $srid = $key;
                $relationId = $item['good_list'][$gid]['relation_id'];
                $sTime = strtotime($start_time . '-01');
                $start_time = (int)date('Ym', $sTime);
                $endTime = (int)date('Ym', strtotime($item['good_list'][$gid]['expire_time'], $sTime));

                $arr['notice'] = $GoodsModel->getSynastryReportUnlockNotice($this->user['id'], $rid, $rid2, $srid, $relationId, $start_time, $endTime);
                break;
            }
        } else
        {
            $arr['notice'] = $GoodsModel->getFortuneUnlockInfo($goods_type, $this->user['id'], $rid, $gid, $start_time);
        }
        return show(config('code.success'), '请求成功', $arr);
    }
}
